<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body style="padding:10px;">
<p>说明：
    <span>for循环体内,循环调用增删改查数据库或查询编号, 提取for外层, 可采用批量查询方式进行一次性集合查询，大大提高性能.</span>
</p>
<br/>
<p>正例：</p>
<div style="margin:10px;padding:10px;border:1px solid #000000;white-space: pre-line;">
    // for循环外定义，for循环直接使用
    Date now = DateUtil.currentDate();
    ShiroUser shiroUser = ShiroKit.getUser();
    // 批量查询编号
    List&lt;String&gt; empNoList = seqService.batchCreateAndUseSeq(EmpConstant.EMP_NO, infos.size());
    // 用集合把新增的数据装载，最后批量插入（修改、删除类似）
    List&lt;EmpInfo&gt; insertEmpInfoList = new ArrayList();

    int i = 0;
    for(EmpInfo empInfo : empInfoList) {
        empInfo.setCreateDate(now);
        empInfo.setUserId(shiroUser.getId());
        // 使用批量方式设置编号
        empInfo.setEmpNo(empNoList.get(i));

        insertEmpInfoList.add(empInfo);
        i ++;
    }

    empInfoService.insertBatch(insertEmpInfoList);
</div>
<br/>
<p>反例：调用admRealTimeRecordService.getByEmpIdAndTime(empInfo.getOriginId(), item.getTime()) 方法,可以抽取写一个集合查询.然后查询对应的记录</p>
<div style="margin:10px;padding:10px;border:1px solid #000000;white-space: pre-line;">
<br/>    // for循环中使用ShiroKit.getUser()
<br/>    for(EmpInfo empInfo : empInfoList) {
<br/>        empInfo.setCreateDate(DateUtil.currentDate());
<br/>        empInfo.setUserId(ShiroKit.getUser().getId());
<br/>    }
<br/>
<br/>
<br/>    List&lt;AdmRealTimeRecord&gt; realTimeRecordList = new ArrayList&lt;&gt;();
<br/>    for(MoneyRecordDTO item :moneyRecordDTOList){
<br/>        if (insertAdmRealTimeRecord.containsKey(item.getTextCard())){
<br/>            continue;
<br/>        }
<br/>        EmpInfo empInfo = empInfoMap.get(item.getCardNo());
<br/>        if (ToolUtil.isEmpty(empInfo)){
<br/>            continue;
<br/>        }
<br/>        // 循环查询
<br/>        AdmRealTimeRecord admRealTimeRecordTemp = admRealTimeRecordService
<br/>        .getByEmpIdAndTime(empInfo.getOriginId(), item.getTime());
<br/>        if (ToolUtil.isNotEmpty(admRealTimeRecordTemp)){
<br/>            continue;
<br/>        }
<br/>        AdmRealTimeRecord admRealTimeRecord = new AdmRealTimeRecord();
<br/>        admRealTimeRecord.setEmpName(empInfo.getName());
<br/>        // 循环插入
<br/>        admRealTimeRecordService.insert(admRealTimeRecord);
<br/>
<br/>        // 循环更新
<br/>        admRealTimeRecordService.updateById(admRealTimeRecord);
<br/>
<br/>        //循环删除
<br/>        admRealTimeRecordService.removeById(admRealTimeRecord);
<br/>
<br/>        // ...
<br/>        insertAdmRealTimeRecord.put(item.getTextCard(), admRealTimeRecord);
<br/>
<br/>    }

</div>
</body>
</html>